package com.zu.commu.auto1_0.protocol;

import com.zu.commu.exception.CommuException;

public class Util {
	public static void main(String[] args) throws CommuException {
		Util u = new Util();
		String s = u.byte2Binary((byte) 100);
		System.out.println(s);
		System.out.println(u.byte2Binary((byte)255));

		int i = -50;
		System.out.println("Number = " + i);
		System.out.println("Hex = " + Integer.toHexString(i&0xFF));
		System.out.println("Double = " + Double.valueOf(7019.0/100));
		System.out.println("double->long:"+Double.valueOf(719.0/100).longValue());
	}

	public double string2Double(String s) {
		double d = Double.parseDouble(s);
		d = Math.round(d * 100.0D) / 100.0D;
		return d;
	}

	public int string2Int(String s) {
		return Integer.parseInt(s);
	}

	public String clean0FromStrNum(String s) {
		while ((s.charAt(0) == '0') && (s.length() > 1)) {
			s = s.substring(1);
		}
		return s;
	}

	public String byte2Hex(byte[] b) throws CommuException {
		String rString = "";
		String temp = "";
		try {
			for (int i = 0; i < b.length; i++) {
				int c = b[i];
				temp = Integer.toHexString(c & 0xFF);
				if (temp.length() == 1) {
					temp = "0" + temp;
				}
				rString = rString + temp;
			}
		} catch (Exception e) {
			throw new CommuException().setMessage("字节数组转换成十六进制的字符串出错！", null);
		}
		return rString;
	}

	public String byte2Hex(byte[] b, int startIndex, int endIndex)
			throws CommuException {
		String rString = "";
		String temp = "";
		try {
			for (int i = startIndex; i <= endIndex; i++) {
				int c = b[i];
				temp = Integer.toHexString(c & 0xFF);
				if (temp.length() == 1) {
					temp = "0" + temp;
				}
				rString = rString + temp;
			}
		} catch (Exception e) {
			throw new CommuException().setMessage("字节数组转换成十六进制的字符串出错！", null);
		}
		return rString;
	}

	public String byte2Binary(byte b) throws CommuException {
		int n = (b + 256) % 256;
		try {
			return Integer.toBinaryString(n);
		} catch (Exception e) {
		}
		throw new CommuException().setMessage("字节转换成二进制的字符串出错！", null);
	}

	public byte[] hex2Byte(String s) throws CommuException {
		int len = s.length() / 2;
		int b = 0;
		int e = 2;
		String[] st = new String[len];
		for (int i = 0; i < len; i++) {
			st[i] = s.substring(b, e);
			b += 2;
			e += 2;
		}
		byte[] bb = new byte[len];
		int i = 0;
		for (int j = 0; j < len; j++) {
			String token = st[j];
			int tokenValue = hex2Int(token);
			byte byteValue = (byte) tokenValue;
			bb[i] = byteValue;
			i++;
		}

		return bb;
	}

	public byte[] int2Byte(String s) throws CommuException {
		int len = s.length() / 2;
		int b = 0;
		int e = 2;
		String[] st = new String[len];
		for (int i = 0; i < len; i++) {
			st[i] = s.substring(b, e);
			b += 2;
			e += 2;
		}
		byte[] bb = new byte[len];
		int i = 0;
		for (int j = 0; j < len; j++) {
			String token = st[j];
			int tokenValue = str2Int(token);
			byte byteValue = (byte) tokenValue;
			bb[i] = byteValue;
			i++;
		}

		return bb;
	}

	private int hex2Int(String s) throws CommuException {
		int result = 0;
		for (int i = 0; i < s.length(); i++) {
			result = (int) (result + Math.pow(16.0D, i)
					* hex2Int(s.charAt(s.length() - 1 - i)));
		}
		return result;
	}

	private int str2Int(String s) throws CommuException {
		return Integer.parseInt(s);
	}

	private int hex2Int(char c) throws CommuException {
		if (c == '0')
			return 0;
		if (c == '1')
			return 1;
		if (c == '2')
			return 2;
		if (c == '3')
			return 3;
		if (c == '4')
			return 4;
		if (c == '5')
			return 5;
		if (c == '6')
			return 6;
		if (c == '7')
			return 7;
		if (c == '8')
			return 8;
		if (c == '9')
			return 9;
		if ((c == 'a') || (c == 'A'))
			return 10;
		if ((c == 'b') || (c == 'B'))
			return 11;
		if ((c == 'c') || (c == 'C'))
			return 12;
		if ((c == 'd') || (c == 'D'))
			return 13;
		if ((c == 'e') || (c == 'E'))
			return 14;
		if ((c == 'f') || (c == 'F'))
			return 15;
		throw new CommuException().setMessage("不是合法的十六进制数！", null);
	}

	public String cleanDotNumber(String value) {
		int dot = value.indexOf('.');
		if (dot < 0) {
			return value;
		}
		if (value.length() - dot - 1 > 2) {
			value = value.substring(0, dot);
		}
		return value;
	}
}
